#!/user/bin/env python
# -*- coding: utf-8 -*-
''' 
Author: Sjy
Description: 显示 等值面 
'''

__all__= []


from PyQt5 import  QtWidgets, QtCore, QtGui
import gui.controls.globalvar as GL
import gui.components.message_box as MBox
import gui.controls.apis as apis
from .ui_isourface import Ui_isourface
from .dlg_isourfaceAverage import DlgIsourfaceAverage


class DlgShowIsourface(QtWidgets.QDialog):
    def __init__(self, *args, **kwargs):
        desktop = GL.get_value("py3d")
        super().__init__(desktop)
        self.ui = Ui_isourface()
        self.ui.setupUi(self)
        self.closeEvent = self.closeEvent
        self.ui.widget.sigAverage.connect(self.popAverage)
        self.popupDlgIsourfaceAverage = DlgIsourfaceAverage(self)
        self.popupDlgIsourfaceAverage.sigGetSeries.connect(self.init_series)
        self.finished.connect(self.closeDialog)
        self.show()
    def closeDialog(self):
        self.popupDlgIsourfaceAverage.sigGetSeries.disconnect()
        dlg = self.popupDlgIsourfaceAverage
        if dlg.isVisible():
            dlg.close()
    def popAverage(self):
        dlg = self.popupDlgIsourfaceAverage
        if dlg.isVisible():
            dlg.raise_()
            dlg.activateWindow()
        else:
            dlg.init()
            dlg.show()
    # 初始化等值面数列
    def init_series(self, series:list=[]):
        # 根据 series 的数据列表 初始化表格数据
        self.ui.set(series)
    # 显示等值面
    def show_isourface(self):
        data = self.ui.get()
        name = GL.get_value(f"threshold.name")
        flag = GL.get_value(f"threshold.flag")
        if not flag or not name :
            MBox.warning('请先设置要查看的类型和名称！')
            return
        apis.enable_contour(data, flag, name)
    @QtCore.pyqtSlot()
    def on_btnApply_clicked(self):
        self.show_isourface()
    @QtCore.pyqtSlot()
    def on_btnRecover_clicked(self):
        apis.disable_contour()
    @QtCore.pyqtSlot()
    def on_btnOk_clicked(self):
        self.show_isourface()
        self.accept()
  
